<?php
namespace app\api\controller;

use app\admin\model\arearecord\Arearecord;
use app\admin\model\dayrechargerecord\Dayrechargerecord;
use app\admin\model\groupchat\Groupchat;
use app\admin\model\hcbaorecord\Hcbaorecord;
use app\admin\model\jianshebaorecord\Jianshebaorecord;
use app\admin\model\level\Level;
use app\admin\model\shouchongjiang\Shouchongjiang;
use app\admin\model\system\System;
use app\admin\model\teamrecord\Teamrecord;
use app\common\controller\Api;
use think\Db;
use think\Exception;
use think\Session;

class Test extends Api{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function index(){
        $moban = 'SMS_327311465';
    }



    public function test(){
        $sms = new AliyunSMS('LTAI5t6uHomgGkytwDtBGN8J', 'TYVpxSxiv8TKNwZ1Lwd6ZyDISbiXf8');

        // 发送验证码
        $result = $sms->sendVerificationCode('15202125654', '123456');

        if ($result['success']) {
            $this->success('短信发送成功，业务ID: '. $result['bizId']);
        } else {
            $this->error('短信发送失败: '. $result['message']);
        }
    }

    public function setlevel(){
        $hcbaoModel = new \app\admin\model\hcbao\Hcbao();
        $userModel = new \app\admin\model\User();

        $systemModel = new System();
        $systeminfo = $systemModel->where(['id'=>1])->find()->toArray();

        $list = collection($hcbaoModel->where(['user_id'=>111])->select());
        print_r($list);exit();


    }


    public function sethcbao(){
        $hcbaoModel = new \app\admin\model\hcbao\Hcbao();
        $list = collection($hcbaoModel->where(['id'=>313])->select());
        if($list){
            $list = $list->toArray();
            $systemModel = new System();
            $userModel = new \app\admin\model\User();
            $systeminfo = $systemModel->where(['id'=>1])->find()->toArray();
            Db::startTrans();
            try{
                foreach ($list as $k=>$v){
                    //写入累积收益
                    $one_shouyi = $v['amount']*$systeminfo['hc_bao']/100;
                    $one_shouyi = $one_shouyi/365;
                    $hcbaoModel->where(['id'=>$v['id']])->setInc('leiji_amount',$one_shouyi);

                    //修改用户信息
                    $userModel->where(['id'=>$v['user_id']])->setInc('hcbao_amount',$one_shouyi);
                    $userModel->where(['id'=>$v['user_id']])->setInc('shouyi_balance',$one_shouyi);

                    $userinfo = $userModel->where(['id'=>$v['user_id']])->find()->toArray();

                    //写入一条HC宝发放纪录
                    $hcbaorecordModel = new Hcbaorecord();
                    $indata['user_id']=$v['user_id'];
                    $indata['all_amount'] = $v['amount'];
                    $indata['amount']=$one_shouyi;
                    $indata['event']='用户'.$userinfo['nickname'].'购买HC宝获取收益'.$one_shouyi;
                    $indata['create_time']=time();
                    $hcbaorecordModel->insert($indata);
                }
                Db::commit();
                $this->success('成功');
            }catch(Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
        }
    }

    public function list(){
        $userModel = new \app\admin\model\User();
        $info = $userModel->where(['id'=>313])->find()->toArray();
        $team_amount = $info['team_amount'];
        $teamrecordModel = new Teamrecord();
        Db::startTrans();
        try{
            //有团队充值收益，按比例拿充值收益
            if($info['team_amount']>0){
                $levelModel = new Level();
                $mylevelinfo = $levelModel->where(['id'=>$info['level']])->find()->toArray();
                $my_amount = $team_amount*$mylevelinfo['bili']/100;
                $userModel->where(['id'=>313])->update([
                    'shouyi_balance'=>$info['shouyi_balance']+$my_amount,
                    'tuandui_amount'=>$info['tuandui_amount']+$my_amount,
                ]);
                //写团队奖纪录
                $indata['user_id'] = $info['id'];
                $indata['team_amount'] = $info['team_amount'];
                $indata['amount'] = $my_amount;
                $indata['bili'] = $mylevelinfo['bili'];
                $indata['event'] = '用户'.$info['nickname'].'达到等级'.$info['level'].'团队收益条件，获取收益'.$my_amount;
                $indata['create_time'] = time();
                $teamrecordModel->insert($indata);
            }
            $idarr=explode(',',$info['pids']);
            if(count($idarr)==1){
                $pinfo = $userModel->where(['id'=>$idarr[0]])->find();
                if($pinfo){
                    $pinfo =$pinfo->toArray();
                    if($pinfo['level']<3){

                    }else{
                        if($pinfo['level']<=$info['level']){
                            $pamount = $info['today_shouyi']*0.1;
                            //上级用户增加余额
                            $userModel->where(['id'=>$pinfo['id']])->update([
                                'shouyi_balance'=>$pinfo['shouyi_balance']+$pamount,
                                'tuandui_amount'=>$info['tuandui_amount']+$pamount,
                            ]);
                            //写团队奖纪录
                            $indata2['user_id'] = $pinfo['id'];
                            $indata2['team_amount'] = $info['today_shouyi'];
                            $indata2['amount'] = $pamount;
                            $indata2['bili'] = 10;
                            $indata2['event'] = '用户'.$pinfo['nickname'].'不满足极差条件拿下家用户'.$info['nickname'].'日收益的10%'.$pamount;
                            $indata2['create_time'] = time();
                            $teamrecordModel->insert($indata2);
                        }else{
                            //计算极差
                            $plevelinfo =  $levelModel->where(['id'=>$pinfo['level']])->find()->toArray();
                            $cha = $plevelinfo['bili']-$mylevelinfo['bili'];
                            $pamount = $team_amount*$cha/100;
                            //上级用户增加余额
                            $userModel->where(['id'=>$pinfo['id']])->update([
                                'shouyi_balance'=>$pinfo['shouyi_balance']+$pamount,
                                'tuandui_amount'=>$info['tuandui_amount']+$pamount,
                            ]);
                            //写团队奖纪录
                            $indata2['user_id'] = $pinfo['id'];
                            $indata2['team_amount'] = $team_amount;
                            $indata2['amount'] = $pamount;
                            $indata2['bili'] = 10;
                            $indata2['event'] = '用户'.$pinfo['nickname'].'满足极差条件拿用户'.$info['nickname'].'团队充值额的'.$cha.'%为'.$pamount;
                            $indata2['create_time'] = time();
                            $teamrecordModel->insert($indata2);
                        }
                    }
                }
            }else{
                $idarr=array_reverse($idarr);
                foreach ($idarr as $k=>$v){
                    $pinfo = $userModel->where(['id'=>$v])->find();
                    if($pinfo){
                        $pinfo =$pinfo->toArray();
                        if($pinfo['level']<3){

                        }else{
                            if($pinfo['level']<=$info['level']){
                                $pamount = $info['today_shouyi']*0.1;
                                //上级用户增加余额
                                $userModel->where(['id'=>$pinfo['id']])->update([
                                    'shouyi_balance'=>$pinfo['shouyi_balance']+$pamount,
                                    'tuandui_amount'=>$info['tuandui_amount']+$pamount,
                                ]);
                                //写团队奖纪录
                                $indata2['user_id'] = $pinfo['id'];
                                $indata2['team_amount'] = $info['today_shouyi'];
                                $indata2['amount'] = $pamount;
                                $indata2['bili'] = 10;
                                $indata2['event'] = '用户'.$pinfo['nickname'].'不满足极差条件拿下家用户'.$info['nickname'].'日收益的10%为'.$pamount;
                                $indata2['create_time'] = time();
                                $teamrecordModel->insert($indata2);
                            }else{
                                //计算极差
                                $plevelinfo =  $levelModel->where(['id'=>$pinfo['level']])->find()->toArray();
                                $cha = $plevelinfo['bili']-$mylevelinfo['bili'];
                                $pamount = $team_amount*$cha/100;
                                //上级用户增加余额
                                $userModel->where(['id'=>$pinfo['id']])->update([
                                    'shouyi_balance'=>$pinfo['shouyi_balance']+$pamount,
                                    'tuandui_amount'=>$info['tuandui_amount']+$pamount,
                                ]);
                                //写团队奖纪录
                                $indata2['user_id'] = $pinfo['id'];
                                $indata2['team_amount'] = $team_amount;
                                $indata2['amount'] = $pamount;
                                $indata2['bili'] = 10;
                                $indata2['event'] = '用户'.$pinfo['nickname'].'满足极差条件拿用户'.$info['nickname'].'团队充值额的'.$cha.'%为'.$pamount;
                                $indata2['create_time'] = time();
                                $teamrecordModel->insert($indata2);
                            }
                        }
                    }
                    $info = $pinfo;
                }
            }
            Db::commit();
            $this->success('发放团队收益成功');
        }catch(Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
    }

    public function checkfanfu(){
        $userModel = new \app\admin\model\User();
        $rechargeModel = new \app\admin\model\recharge\Recharge();
        $user_id = 159;
        $userinfo = $userModel->where(['id'=>159])->find()->toArray();
        echo '159用户的直推人数为：'.$userinfo['zhituinums'];
        echo "\r\n";

        $list = collection($userModel->where(['pid'=>$user_id])->select())->toArray();
        foreach ($list as $k=>$v){
            $sum = $rechargeModel->where(['user_id'=>$v['id'],'status'=>1])->sum('amount');
            echo '用户'.$v['id'].'的总充值额度为'.$sum;echo "\r\n";
        }
        exit();
    }

    public function getwithcode(){
        echo Session::get('WITHDRAW_CODE');
    }

    public function tui(){
        $userModel = new \app\admin\model\User();
        $levelModel = new \app\admin\model\level\Level();
        $levelinfo = $levelModel->where(['id'=>1])->find()->toArray();
        $withdrawModel = new \app\admin\model\withdraw\Withdraw();

        $systemModel = new System();
        $systeminfo = $systemModel->where(['id'=>1])->find()->toArray();

        $rechargeModel = new \app\admin\model\recharge\Recharge();
        //$where['zhituinums'] = ['>=',2];
        //$where['team_amount'] = ['>=',$levelinfo['amount']];
        $list = collection($userModel->where(['id'=>136])->select());
        $teamrecordModel = new Teamrecord();
        if($list){
            $list = $list->toArray();
            Db::startTrans();
            try{
                foreach ($list as $k=>$v){
                    if($v['team_amount'] > 0){
                        $one = $v['team_amount']/365*$levelinfo['bili']/100;
                        //修改用户收益和团队奖励
                        $userModel->where(['id'=>$v['id']])->update([
                            'shouyi_balance'=>$v['shouyi_balance']+$one,
                            'tuandui_amount'=>$v['tuandui_amount']+$one,
                            'today_team_shouyi'=>$one
                        ]);

                        //写入团队奖纪录
                        $indata['user_id'] = $v['id'];
                        $indata['team_amount'] = $v['team_amount']/365;
                        $indata['amount'] = $one;
                        $indata['bili']=$levelinfo['bili'];
                        $indata['event']='用户'.$v['nickname'].'等级达到战士获取团队奖励'.$one;
                        $indata['create_time'] = time();
                        $teamrecordModel->insert($indata);
                    }
                }
                Db::commit();
                $this->success('发放团队收益成功');
            }catch(Exception $e){
                Db::rollback();
                $this->error($e->getMessage());
            }
        }
    }

    public function setqun(){
        $groupchatModel = new Groupchat();
        $grouplist = collection($groupchatModel->where([])->select())->toArray();
        $idarr = [];
        foreach ($grouplist as $k=>$v){
            array_push($idarr,$v['user_id']);
        }
        $idarr=array_unique($idarr);
        $idstr = implode(',',$idarr);
        $qunModel = new \app\admin\model\qun\Qun();
        $qunModel->where(['id'=>1])->update([
            'chengyuan'=>$idstr,
        ]);
        echo '成功';exit();
    }

    public function setusers(){
        $jianshebaorecordModel = new Jianshebaorecord();
        $userModel = new \app\admin\model\User();
        $list = collection($jianshebaorecordModel->where([])->select())->toArray();
        Db::startTrans();
        try{
            foreach ($list as $k=>$v){
                $info = $userModel->where(['id'=>$v['user_id']])->find();
                if($info){
                    $info = $info->toArray();
                    $userModel->where(['id'=>$info['id']])->update([
                        'shouyi_balance'=>$info['shouyi_balance']+$v['amount'],
                    ]);
                    $jianshebaorecordModel->where(['id'=>$v['id']])->delete();
                }
            }
            Db::commit();
            $this->success('成功');
        }catch (Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
    }
}